package com.company.test30;

import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

/**
 * Created by v on 16-12-1.
 */
public class TicTacToeClient extends JApplet implements TicTacToeConstants{
    //玩家属于什么玩家
    private int player;

    //玩家用的棋子
    private int target;

    //游戏的格子
    private Cell[][] cells=new Cell[3][3];

    //用于显示游戏状态
    private JLabel label=new JLabel("正在初始化请稍后……");

    //棋盘上的状态
    private int state;
    //是否轮到你
    private boolean isYourTurn=false;

    private DataInputStream input;
    private DataOutputStream output;

    @Override
    public void init() {
        for(int i=0;i<cells.length;i++){
            for(int j=0;j<cells[i].length;j++){
                cells[i][j]=new Cell(i*3+j);
            }
        }

        //更改一个好看的字体
        label.setFont(new Font("微软雅黑",0,20));

        //把格子放进一个容器内
        JPanel panel=new JPanel();
        panel.setLayout(new GridLayout(3,3));
        for(int i=0;i<3;i++){
            for(int j=0;j<3;j++){
                panel.add(cells[i][j]);
            }
        }

        //设置边界
        panel.setBorder(new LineBorder(Color.RED,1));
        label.setBorder(new LineBorder(Color.BLACK,1));
        setLayout(new BorderLayout());

        //加入到this中
        add(panel,BorderLayout.CENTER);
        add(label,BorderLayout.SOUTH);

        label.setText("正在等待玩家，请稍后……");
        connectToServer();
    }

    public void connectToServer(){
        try{
            Socket socket=new Socket("localhost",8000);

            input=new DataInputStream(socket.getInputStream());
            output=new DataOutputStream(socket.getOutputStream());

            Thread thread=new Thread(new PlayChess());
            thread.start();
        }catch (IOException ex){
            ex.printStackTrace();
        }
    }

    private class PlayChess implements Runnable{
        @Override
        public void run() {
            try{
                player=input.readInt();
                target=input.readInt();
                if(target==X){
                    label.setText("先由对方下棋");
                    int next=input.readInt();
                    otherPlayChess(next);
                    label.setText("轮到你了");
                }else {
                    label.setText("先由你来下棋");
                }

                state=input.readInt();
                while (state==CONTINUE){
                    isYourTurn=true;
                    label.setText("轮到你啦");
                    int next=input.readInt();
                    if (next!=-1){
                        otherPlayChess(next);
                    }
                    state=input.readInt();
                }

                if(state==DRAW){
                    label.setText("平局");
                }else if(state==player){
                    label.setText("恭喜你赢得了胜利");
                }else {
                    label.setText("很遗憾，你输了");
                }
            }catch (Exception ex){
                ex.printStackTrace();
            }
        }
    }

    public void otherPlayChess(int next){
        if(target==O){
            cells[next/3][next%3].target=X;
        }else {
            cells[next/3][next%3].target=O;
        }
        cells[next/3][next%3].repaint();
    }

    private class Cell extends JPanel{
        private int target=0;
        private int number;

        public Cell(int number){
            this.number=number;
            setBorder(new LineBorder(Color.black,1));
            addMouseListener(new MyMouseListener());
        }

        private class MyMouseListener extends MouseAdapter {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                if (!isYourTurn||target!=0){
                    return;
                }
                try{
                    output.writeInt(number);
                    target=TicTacToeClient.this.target;
                    repaint();
                    System.out.println(target);
                    label.setText("请等待对手下棋");
                }catch (IOException ex){
                    ex.printStackTrace();
                }finally {
                    isYourTurn=false;
                }
            }
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if(target==X){
                g.drawLine(10,10,getWidth()-10,getHeight()-10);
                g.drawLine(getWidth()-10,10,10,getHeight()-10);
            }else if(target==O){
                g.drawOval(10,10,getWidth()-20,getHeight()-20);
            }
        }
    }
}
